//
//  LocalHelper.m
//  LocalTest
//
//  Created by Pibinuo on 15/12/3.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "LocalHelper.h"

@interface LocalHelpers ()
@property(nonatomic,strong)NSMutableArray * dataArray;
@end

@implementation LocalHelpers

+ (LocalHelpers *)defaultHelpers
{
    static dispatch_once_t onceToken;
    static LocalHelpers * newsHelpers = nil;
    dispatch_once(&onceToken, ^{
        newsHelpers = [LocalHelpers new];
        newsHelpers.dataArray = [NSMutableArray array];
        newsHelpers.loopArray = [NSMutableArray array];
    });
    return newsHelpers;
}

-(void)reloadData
{
    if (self.urlStr == nil) {
        self.urlStr = @"http://api.irecommend.ifeng.com/local.php?&choicetype=city&choicename=北京";
    }
   NSURL * url = [NSURL URLWithString: [self.urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//    NSURL * url1 = [NSURL URLWithString:@"http://api.irecommend.ifeng.com/local.php?&choicetype=city&choicename=包头"];
    NSURLSession * session = [NSURLSession sharedSession];
    NSLog(@"self.url  ===  %@",self.urlStr);
    NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data != nil) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSDictionary * dataDic= dic[@"data"];
            NSDictionary * listDic = dataDic[@"list"];
            NSArray * itemArray = listDic[@"item"];
            for (NSDictionary * dict in itemArray) {
                News * new = [[News alloc]init];
                [new setValuesForKeysWithDictionary:dict];
                new.updateTime = [self timeWithUpdateTime:new.updateTime];
                [self.dataArray addObject:new];
            }
         
            dispatch_async(dispatch_get_main_queue(), ^{
               
                [[NSNotificationCenter defaultCenter] postNotificationName:@"finishNews" object:nil];
                
            });
            
        }
    }];
    [task resume];
}


-(NSString *)timeWithUpdateTime:(NSString *)updataTime
{
    if (updataTime != nil) {
        NSDate * nowDate = [NSDate date];
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
        NSDate * newDate = [formatter dateFromString:updataTime];
        NSTimeInterval interval = [nowDate timeIntervalSinceDate:newDate];
        //NSLog(@"%f", interval);
        if (interval <  60 * 5) {
            return @"刚刚";
        }else if (interval > 60 * 5&& interval < 60 * 60){
            return [NSString stringWithFormat:@"%.f分钟前", interval / 60];
        }else if (interval > 60 * 60 && interval < 60 * 60 * 3){
            return [NSString stringWithFormat:@"%.f小时前", interval / 60 / 60];
        }else if (interval > 60 * 60 * 3 && interval < 60 * 60 * 24){
            
            //设置为23：00格式
            //NSDate * date = [NSDate dateWithTimeInterval:24 * 60 * 60 sinceDate:newsDate];
            NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"HH:mm"];
            NSString * dateStr = [formatter stringFromDate:newDate];
            
            return [NSString stringWithFormat:@"%@", dateStr];
        }else if (interval > 60 * 60 * 24 && interval < 60 * 60 * 30 * 24){
            return [NSString stringWithFormat:@"%.f天前", interval / 60 / 60 / 24];
        }else if (interval > 60 * 60 * 24 * 30){
            return [NSString stringWithFormat:@"%.f月前", interval / 60 / 60 / 24 / 30];
        }
    }
    return nil;
}

-(void)removeAll
{
    [self.dataArray removeAllObjects];
}

-(NSInteger)modelCount
{
    return self.dataArray.count;
}

-(News *)modelWithIndex:(NSInteger)index
{
    if (self.dataArray != nil) {
     News * new = self.dataArray[index];
    return new;   
    }
    return nil;
}






@end
